package com.whut.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whut.mapper.CheckgroupMapper;
import com.whut.mapper.GroupMemberDetailMapper;
import com.whut.mapper.GroupmemberMapper;
import com.whut.mapper.UserinfoMapper;
import com.whut.model.Checkgroup;
import com.whut.model.Groupmember;
import com.whut.service.GroupService;
import com.whut.v_model.GroupMemberDetail;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service("groupService")
public class GroupServiceImpl implements GroupService {

    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private GroupMemberDetailMapper groupMemberDetailMapper;
    @Resource
    private GroupmemberMapper groupmemberMapper;
    @Resource
    private CheckgroupMapper checkgroupMapper;

    @Override
    public Object getAllCandidateUser() {
        return userinfoMapper.getAllUserInfo();
    }

    @Override
    public Object getOtherCandidate(int groupId) {
        return userinfoMapper.selectOtherCandidate(groupId);
    }

    @Override
    public Object getGroupByPage(int pageNum, int pageSize) {
        List<GroupMemberDetail> list0 = groupMemberDetailMapper.selectAllGroup("Y");
//        PageHelper.startPage(pageNum, pageSize);
//        List<GroupMemberDetail> list = groupMemberDetailMapper.selectAllGroup("Y");
//        List<GroupMemberDetail> targetList = new ArrayList<>();
//        Set<Integer> set = new HashSet<Integer>();
//        for (GroupMemberDetail item:list) {
//            if(!set.contains(item.getGroupId())){
//                targetList.add(item);
//                set.add(item.getGroupId());
//            }else {
//                String name = targetList.get(targetList.size()-1).getUserName();
//                targetList.get(targetList.size()-1).setUserName(name+"-"+item.getUserName());
//            }
//        }
//        PageInfo result = new PageInfo(list);
//        result.setTotal(list0.size());
//        System.out.println(list.size()+' '+list0.size()+' ');
        Map<String,List<GroupMemberDetail>> result = new HashMap<>();
        result.put("data",list0);
        return result;
    }

    @Override
    public Object getGroupMembersByGroupId(int groupId) {//根据小组Id查找小组所有成员
        return groupMemberDetailMapper.selectAllGroupMemberByGroupId(groupId);
    }

    @Override
    public String deleteGroupMember(int groupId, int userId) { //去小组成员表删除一条记录

        try{
            groupmemberMapper.deleteGroupMember(groupId,userId);
            return "success";
        }catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public String deleteGroupById(int groupId) {
        //1-删除小组表
        //2-删除小组成员表
        try{
            checkgroupMapper.deleteByPrimaryKey(groupId);
            groupmemberMapper.deleteGroupMemberByGroupId(groupId);
            return "success";
        }catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public String addTeam(String groupName, List<Groupmember> teamMembers) {//添加小组
        //先增加组
        try{
            Checkgroup checkgroup = new Checkgroup();
            checkgroup.setGroupName(groupName);
            checkgroupMapper.insert(checkgroup);
            for(int i = 0 ; i < teamMembers.size() ; i++) {
                teamMembers.get(i).setGroupId(checkgroup.getGroupId());
            }
            groupmemberMapper.insertMembers(teamMembers);
            return "success";
        }catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public String addGroupMember(List<Groupmember> teamMembers) {
        try{
            groupmemberMapper.insertMembers(teamMembers);
            return "success";
        }catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public Object getUserByVagueName(String userName) {
        return userinfoMapper.getUserByVagueName(userName);
    }

    @Override
    public int setHeader(Integer userId, Integer groupId){
        System.out.println("成功更新");
       return groupmemberMapper.setHeader(userId,groupId);
    }

    @Override
    public int cancelHeader(int userId, int groupId) {
        System.out.println("成功更新");
        return groupmemberMapper.cancelHeader(userId,groupId);
    }

    @Override
    public int countHeader(Integer groupId){
        return groupmemberMapper.countHeader(groupId);
    }

    @Override
    public String discardByGroupId(int groupId) {
        checkgroupMapper.discardByGroupId(groupId);
        return "success";
    }

    @Override
    public String freeByGroupId(int groupId) {
        checkgroupMapper.freeByGroupId(groupId);
        return "success";
    }

    @Override
    public Object getGroupInfo() {
        List<GroupMemberDetail> list0 = groupMemberDetailMapper.getGroupInfo();
        Map<String,List<GroupMemberDetail>> result = new HashMap<>();
        result.put("data",list0);
        return result;
    }

}
